package com.nbjtjc.safe.service.wechat;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.nbjtjc.safe.bean.CompanySelector;
import com.nbjtjc.safe.bean.Constant;
import com.nbjtjc.safe.model.sys.SysDept;
import com.nbjtjc.safe.service.sys.SysDeptService;
import com.nbjtjc.safe.util.MyShiroUtils;
import org.activiti.engine.*;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @auther guqianbin
 * @date 2018/10/11 10:54
 **/
@Service
public class WechatDangerService {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private TaskService taskService;
    @Autowired
    IdentityService identityservice;
    @Autowired
    RuntimeService runtimeservice;

    @Autowired
    HistoryService historyService;

    @Autowired
    private SysDeptService sysDeptService;

    /**
     * 小程序 返回隐患检查的四类任务的数量
     *
     * 待检查
     * 发布整改单
     * 整改
     * 整改验收
     */
    public Map<String,Long> dangerTaskTypeCount(){
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey("dangerProcess").latestVersion().singleResult();

        long unCheckCount=taskService.createTaskQuery()
                .taskAssignee(MyShiroUtils.getSessionUserId().toString())
                .processDefinitionKey(processDefinition.getKey())
                .taskDefinitionKey("usertask2").count();

        long reformSheet=taskService.createTaskQuery()
                .taskAssignee(MyShiroUtils.getSessionUserId().toString())
                .processDefinitionKey(processDefinition.getKey()).taskDefinitionKey("usertask4")
                .count();

        long reform=taskService.createTaskQuery()
                .taskAssignee(MyShiroUtils.getSessionUserId().toString())
                .processDefinitionKey(processDefinition.getKey()).taskDefinitionKey("usertask7")
                .count();

        long checkAccept=taskService.createTaskQuery()
                .taskAssignee(MyShiroUtils.getSessionUserId().toString())
                .processDefinitionKey(processDefinition.getKey()).taskDefinitionKey("usertask9")
                .count();

        Map<String,Long> data=new HashMap<>();

        data.put("unCheckCount",unCheckCount);
        data.put("reformSheet",reformSheet);
        data.put("reform",reform);
        data.put("checkAccept",checkAccept);
        return data;
    }

    /**
     *  根据给的节点定义，统计应急任务的数量
     * @param taskDefineKeys 需要統計的任務的key
     * @return
     */
    public Map<String,Long> emgTaskCount(List<String> taskDefineKeys,String processDefinitionKey){
        Map<String,Long> data=new HashMap<>();
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionKey(processDefinitionKey).latestVersion().singleResult();
        if(ObjectUtil.isNotNull(processDefinition) && CollectionUtil.isNotEmpty(taskDefineKeys)){
            taskDefineKeys.forEach(taskDefineKey->{
                long count=taskService.createTaskQuery()
                        .taskAssignee(MyShiroUtils.getSessionUserId().toString())
                        .processDefinitionKey(processDefinitionKey)
                        .taskDefinitionKey(taskDefineKey).count();
                data.put(taskDefineKey,count);
            });
        }
        return data;
    }

    public List<CompanySelector> getCompanySelectors(){
        List<SysDept> sysDepts= sysDeptService.getLowerCompanyDepts();
        List<CompanySelector> companySelectors=new ArrayList<>();
        sysDepts.forEach(sysDept -> {
            if(sysDept.getType().equals(Constant.SysDeptType.COMPANY.getValue())){
                CompanySelector companySelector=new CompanySelector();
                companySelector.setId(sysDept.getId());
                companySelector.setText(sysDept.getName());
                compareSysDept(sysDept.getSubDept(),companySelector);
                companySelectors.add(companySelector);
            }

        });
        return companySelectors;
    }

    private void compareSysDept(List<SysDept> sysDepts, CompanySelector companySelector){
        if(CollectionUtil.isNotEmpty(sysDepts)){
            List<CompanySelector> selectors=new ArrayList<>();
            sysDepts.forEach(sysDept -> {
                if(sysDept.getType().equals(Constant.SysDeptType.COMPANY.getValue())){
                    CompanySelector selector=new CompanySelector();
                    selector.setId(sysDept.getId());
                    selector.setText(sysDept.getName());
                    compareSysDept(sysDept.getSubDept(),selector);
                    selectors.add(selector);
                }
            });
            companySelector.setChildren(selectors);
        }
    }
}
